package controller;


import com.github.pagehelper.PageInfo;
import entity.*;
import mapper.LinkmanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CustomerService;
import service.Sale_opportunityService;
import service.UsersService;

import java.util.List;

/**
 * @Author Administrator
 * @create 2020.06.22 下午 8:10
 */
@Controller
@RequestMapping("/sales")
public class SalesController {
    @Autowired
    private Sale_opportunityService sale_op;
    @Autowired
    private UsersService usersService;
    @Autowired
    private LinkmanMapper linkmanMapper;
    @Autowired
    private CustomerService customerService;
    @ResponseBody
    @GetMapping("/list")
    public MyResult<List<Sale_opportunity>> layList(LayRequest layRequest){
        PageInfo<Sale_opportunity> rolePageInfo=sale_op.selectByMap(layRequest);
        return new MyResult<>(0,rolePageInfo.getTotal(),rolePageInfo.getList());
    }

    @RequestMapping("toAdd")
    public String  toAdd(ModelMap modelMap){
        List<Users> users = usersService.selectAll();
        List<Linkman> linkman = linkmanMapper.selectAll();
        List<Customer> customer = customerService.selectAll();
        modelMap.addAttribute("us",users);
        modelMap.addAttribute("li",linkman);
        modelMap.addAttribute("cu",customer);
        return "add.jsp";
    }


    @ResponseBody
    @RequestMapping("/add")
    public MyResult insert(Sale_opportunity sale_opportunity){
        boolean b = sale_op.insert(sale_opportunity);
        return new MyResult(0);
    }


    @RequestMapping("/delete")
    @ResponseBody
    public MyResult delete(Integer id){
        boolean flag= sale_op.delete(id);
        return new MyResult<>(0);
    }

    @RequestMapping("/pick")
    @ResponseBody
    public MyResult pick(Integer id,String status){
        if (status.equals("锁定")) {
            return new MyResult<>(1);
        }
        boolean flag= sale_op.updatestatus(id);
        return new MyResult<>(0);
    }
}
